package com.spring.framework.transaction.listener;

import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.io.FileNotFoundException;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author xuweizhi
 */
@EnableAsync
@Configuration
@SuppressWarnings("all")
@ComponentScan(basePackages = {"com.spring.framework.transaction.listener"})
public class TransactionEventListenerConfig {

	/**
	 * 配置了一个Spring的ThreadPoolTaskExecutor线程池，用于执行异步任务
	 */
	@Bean
	public ThreadPoolTaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		//配置核心线程数
		executor.setCorePoolSize(5);
		//配置最大线程数
		executor.setMaxPoolSize(10);
		//配置队列大小
		executor.setQueueCapacity(800);
		//配置线程池中的线程的名称前缀
		executor.setThreadNamePrefix("TransactionalExecutor-");
		// rejection-policy：拒绝策略，由调用者所在的线程来执行
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
		return executor;
	}


	public static void main(String[] args) throws FileNotFoundException {
		AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(TransactionEventListenerConfig.class);
		EventService eventService = applicationContext.getBean(EventService.class);
		eventService.select();
	}


}


